<?php defined('SYSPATH') or die('No direct script access.');

abstract class Model_Specs_Abstract extends ORM
{ 
    protected $_reload_on_wakeup = false;
    protected $_visible_filed    = 'visible';  
    
    public static function get_model_type(Model_Model $model)
    {
        if ($model->category_id == 396) {
            return 'camera';    
        }    
        return null;
    }
    
    public function get_spec_data($key, $rules, $compare_data)
    {
        $value = $original_value = $this->__get($key);
        $method_name = '_get_'.$key.'_value';
        if (method_exists($this, $method_name)) {
            $value = call_user_func(array($this, $method_name), $rules);     
        } else {
            if (isset($rules['visible_field']) && $rules['visible_field']) {
                $value = $this->__get($key.'_'.$this->_visible_filed);        
            }
            if (isset($rules['suffix']) && $rules['suffix']) {
                $value .= $rules['suffix'];   
            }
            if (isset($rules['prefix']) && $rules['prefix']) {
                $value = $rules['prefix'].$value;   
            }
            if (isset($rules['boolean']) && $rules['boolean']) {
                $value = 'нет'; 
                if ($original_value) {
                    $value = 'есть';
                } 
            }
        }
        $compare = array();
        if (isset($rules['comparable']) && $rules['comparable']) {
            $compare_data = isset($compare_data[$key])?$compare_data[$key]:null;
            if ($compare_data) {
                $found = false;
                foreach ($compare_data as $item) {
                    if ($item['value'] == $original_value) {
                        $compare_data = $item;
                        $found = true;
                        break;        
                    }        
                }
                if ($found) { 
                    // от задачи минимизации к задаче максимизации
                    if (isset($rules['reverse']) && $rules['reverse']) {
                        $original_value = -$original_value;
                        
                        $tmp = $compare_data['min'];
                        $compare_data['min'] = -$compare_data['max'];
                        $compare_data['max'] = -$tmp;
                        
                        $tmp = $compare_data['absolute_min'];
                        $compare_data['absolute_min'] = -$compare_data['absolute_max'];
                        $compare_data['absolute_max'] = -$tmp;           
                    }                                         
                    $compare = array(
                        'value'                 => 0,
                        'absolute_value'        => 0,
                        'count_better'          => $compare_data['count_better'],
                        'count_same'            => $compare_data['count_same'],
                        'count_worse'           => $compare_data['count_worse'],
                        'absolute_count_better' => $compare_data['absolute_count_better'],
                        'absolute_count_same'   => $compare_data['absolute_count_same'],
                        'absolute_count_worse'  => $compare_data['absolute_count_worse'],
                        /*'original_value'        => $original_value,
                        'min'                   => $compare_data['min'],
                        'max'                   => $compare_data['max'],
                        'amin'                  => $compare_data['absolute_min'],
                        'amax'                  => $compare_data['absolute_max'],*/
                    );
                    // нормализация критериев
                    if (isset($rules['boolean']) && $rules['boolean']) {
                        $compare['value'] = $compare['absolute_value'] = 0;
                        if ($original_value) {
                            $compare['value'] = $compare['absolute_value'] = 100;    
                        }    
                    } else {
                        if ($tmp = ($compare_data['max'] - $compare_data['min'])) {
                            $compare['value'] = round(($original_value - $compare_data['min'])/$tmp, 2)*100;
                        }
                        if ($tmp = ($compare_data['absolute_max'] - $compare_data['absolute_min'])) {
                            $compare['absolute_value'] = round(($original_value - $compare_data['absolute_min'])/$tmp, 2)*100;
                        }
                    }    
                    if (isset($rules['weight']) && $rules['weight']) {
                        $compare['weight'] = $rules['weight'];   
                    }
                }        
            }        
        }
        
        $result = array(
            'name'  => $rules['name'],
            'value' => $value,
        );
        if (!empty($compare)) {
            $result['compare'] = $compare;    
        }
        return $result;
    }
    
    /**
    * Получаем массив с возможными хар-ками и настройками для их отображения
    * 
    */
    abstract function get_available_specs(); 
    
    /**
    * Получить массив с хар-ками и готовыми значениями для сравнения
    * 
    * @param Model_Model $model
    */
    abstract function get_specs_full_data($model); 
    
    /**
    * Получить необработанные значения для сравнения для get_specs_full_data
    * 
    * @param Model_Specs_Camera $spec
    */
    abstract public function get_compare_data($spec);  
    
    /**
    * Сравнение 2х моделей
    * 
    * @param Model_Model $model1
    * @param Model_Model $model2
    */
    abstract public function compare($model1, $model2, $competitors, $winner);
}
